﻿using System;
using System.Windows;
using ULSDeobfuscator.MVVM.Controls;

namespace Elca.MvvmHelpers {
    public class UserInteraction : IMessageToUser {
        /// <summary>
        /// Asks a question to the user
        /// </summary>
        /// <param name="caption">Title of the question</param>
        /// <param name="question">Question text</param>
        /// <param name="qt">Question type</param>
        /// <returns>User answer</returns>
        public AnswerType AskQuestion(string caption, string question, QuestionType qt) {

            MessageBoxButton mbb = MessageBoxButton.OK;

            switch (qt) {

                case QuestionType.OKCancel:
                    mbb = MessageBoxButton.OKCancel;
                    break;

                case QuestionType.YesNo:
                    mbb = MessageBoxButton.YesNo;
                    break;

                case QuestionType.YesNoCancel:
                    mbb = MessageBoxButton.YesNoCancel;
                    break;
            }

            MessageBoxResult dr = MessageBox.Show(Application.Current.MainWindow, question, caption, 
                                                  mbb, MessageBoxImage.Question);

            AnswerType at = AnswerType.Cancel;
            switch (dr) {
                
                case MessageBoxResult.No:
                    at = AnswerType.No;
                    break;

                case MessageBoxResult.Yes:
                    at = AnswerType.Yes;
                    break;

                case MessageBoxResult.OK:
                    at = AnswerType.OK;
                    break;
            }

            return at;
        }

        /// <summary>
        /// Shows an information message to the user
        /// </summary>
        /// <param name="caption">Message title</param>
        /// <param name="text">Message text</param>
        public void ShowInformation(string caption, string text) {
            MessageBox.Show(Application.Current.MainWindow, text, caption, MessageBoxButton.OK,
                            MessageBoxImage.Information);
        }

        /// <summary>
        /// Shows a warning message to the user
        /// </summary>
        /// <param name="caption">Message title</param>
        /// <param name="text">Message text</param>
        public void ShowWarning(string caption, string text) {
            MessageBox.Show(Application.Current.MainWindow, text, caption, MessageBoxButton.OK,
                            MessageBoxImage.Warning);
        }

        /// <summary>
        /// Shows an error message to the user
        /// </summary>
        /// <param name="caption">Message title</param>
        /// <param name="text">Message text</param>
        public void ShowError(string caption, string text) {
            MessageBox.Show(Application.Current.MainWindow, text, caption, MessageBoxButton.OK,
                            MessageBoxImage.Error);
        }

        public string Prompt(string caption, string text, string initialValue = null) {

            PromptWindow pw = new PromptWindow {
                Caption = caption,
                Text = text,
                Answer = initialValue,
                Owner = Application.Current.MainWindow,
            };

            if (pw.ShowDialog() == true) {
                return pw.Answer;
            }

            return null;
        }
    }
}
